package main.mvc;

import static main.mvc.utils.BuildHelper.buildEntityName;
import static main.mvc.utils.BuildHelper.getImport;
import static main.mvc.utils.BuildHelper.getNAndT;
import static main.mvc.utils.BuildHelper.getPropertiesparse;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

public class DaoInterfaceBuilder {
	
	public static void buildDao(String tableName) {
		
		String entityName = buildEntityName(tableName, true);
		String subName = buildEntityName(tableName, false);
		/*
		 * package ...
		 * import java.util.List;
		 * import ....
		*/
		String headStr = buildHead(entityName);
		// public interface BookDao {
		String daoStr = buildDaoStr(entityName);
		// insert(Entity entity);
		String insertStr = buildInsertStr(entityName, subName);
		// update(Entity entity);
		String updateStr = buildUpdateStr(entityName, subName);
		// delete(Entity entity);
		String deleteStr = buildDeleteStr(entityName, subName);
		// getById(Integer id);
		String getByIdStr = buildGetByIdStr(entityName);
		// getList
		String getListStr = buildGetListStr(entityName);
		// getpage
		String getPageStr = buildQueryForPageStr(entityName);
		
	String _java = headStr + daoStr + insertStr + updateStr + deleteStr + getByIdStr + getListStr + getPageStr + "\n}";
//	System.out.println(_java);
		
		String folderName = getPropertiesparse().getValue("dao.build.path");
		String fileName = getPropertiesparse().getValue("dao.build.path") + File.separator + entityName + "Dao.java";
		String isUpdate = getPropertiesparse().getValue("dao.build.rebuild");
		
		try {
			File folder = new File(folderName);
			if(!folder.exists()){
				folder.mkdirs();
			}
			
			File file = new File(fileName);
			if(!file.exists()){
				file.createNewFile();
				BufferedWriter out = new BufferedWriter(new FileWriter(file));
				out.write(_java);
				out.close();
				System.out.println("dao is not exist");
			} else {
				if(isUpdate.equals("true")){
					file.delete();
					BufferedWriter out = new BufferedWriter(new FileWriter(file));
					out.write(_java);
					out.close();
				} 
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	/** package ... import ... */
	public static String buildHead(String entityName) {
		StringBuffer head = new StringBuffer();
		head.append("package " + getPropertiesparse().getValue("dao.build.package") + ";" + getNAndT(2, 0));
		head.append(getImport(1) + getNAndT(2, 0));
		
		head.append(getImport(6) + entityName + ";" + getNAndT(1, 0));
		head.append(getImport(15) + getNAndT(1, 0));
		head.append(getImport(16) + getNAndT(1, 0));
		head.append(getImport(2));
		
		return head.toString();
	}
	
	/** public interface XXXDao {  */
	public static String buildDaoStr(String entityName) {
		return getNAndT(2, 0) + "public interface " + entityName + "Dao" + " {";
	}
	
	/** public boolean insert(Entity entity); */
	public static String buildInsertStr(String entityName, String subName) {
		return getNAndT(2, 1) + "public boolean insert(" + entityName + " " + subName + ") throws DaoException;";
	}

	/** public boolean update(Entity entity); */
	public static String buildUpdateStr(String entityName, String subName) {
		return getNAndT(2, 1) + "public boolean update(" + entityName + " "	+ subName + ") throws DaoException; ";
	}
	
	/** public boolean detele(Integer id); */
	public static String buildDeleteStr(String entityName, String subName) {
		return getNAndT(2, 1) + "public boolean delete(Integer id) throws DaoException;";
	}
	
	/** public Entity getEntityById(Integer id); */
	public static String buildGetByIdStr(String entityName) {
		return getNAndT(2, 1) + "public " + entityName + " getById(Integer id) throws DaoException;";
	}
	
	/** public List<Entity> getEntityList(Integer id); */
	public static String buildGetListStr(String entityName) {
		return getNAndT(2, 1) + "public List<" + entityName + "> queryForList() throws DaoException;";
	}
	
	public static String buildQueryForPageStr(String entityName) {
		return getNAndT(2, 1) + "public PageView<" + entityName + "> queryForPage(Page page) throws DaoException;";
	}
	
	public static void main(String args[]){
		
		buildDao("ssssss");
		
	}
	
}
